import tkinter as tk
from tkinter import messagebox
import requests

API_URL = "https://api.deepseek.com/v1/chat/completions"
API_KEY = ""

# 定义发送请求的函数
def send_request():
    user_input = entry.get()
    if not user_input:
        messagebox.showwarning("输入错误", "请输入文本")
        return

    # 清空文本框并显示生成中
    result_text.delete(1.0, tk.END)
    result_text.insert(tk.END, "生成中...")
    root.update_idletasks()

    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    }

    data = {
        "model": "deepseek-chat",
        "messages": [{"role": "user", "content": user_input}]
    }

    try:
        response = requests.post(API_URL, headers=headers, json=data)
        response.raise_for_status()
        result = response.json()
        # 清空文本框并显示结果
        result_text.delete(1.0, tk.END)
        result_text.insert(tk.END, result["choices"][0]["message"]["content"])
        print(result["choices"][0]["message"]["content"])
    except requests.exceptions.RequestException as e:
        # 清空文本框并显示错误信息
        result_text.delete(1.0, tk.END)
        result_text.insert(tk.END, f"请求失败: {e}")

# 创建主窗口
root = tk.Tk()
root.title("AI ASSISTANT")
root.geometry("400x400")

# 创建输入框
entry_label = tk.Label(root, text="请输入文本:")
entry_label.pack(pady=10)
entry = tk.Entry(root, width=50)
entry.pack(pady=10)

# 创建按钮
button = tk.Button(root, text="发送", command=send_request)
button.pack(pady=20)

# 创建用于显示结果的文本框
result_text = tk.Text(root, height=10, width=50)
result_text.pack(pady=20)

# 运行主循环
root.mainloop()